#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 22, 2010

@author: lxd
'''
import wx
from common.tools import Tools
import images

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, size=(500, -1), title=u'迷你博客留言管理', style=wx.DEFAULT_FRAME_STYLE)
        self.palMain = panelMessage(self)      
        self.Center()        
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        
    def OnClose(self, event):
        new_messages = self.palMain.lstMessage.GetItems()
        messages = Tools.loadData(r'db/tmessage') 
        if new_messages:       
            if (messages and new_messages != messages) or not messages: 
                dlg = wx.MessageDialog(None, u'迷你博客留言已更改，要保存吗？', u'友情提示', wx.YES_NO | wx.CANCEL | wx.ICON_QUESTION)
                retCode = dlg.ShowModal()    
                if retCode == wx.ID_YES:
                    Tools.saveData(r'db/tmessage', new_messages)
                    self.Destroy()
                elif retCode == wx.ID_NO:
                    self.Destroy()                    
            else:
                self.Destroy()                    
        else:
            self.Destroy()
                    
class panelMessage(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent) 
        #ttooooooooooooooooooooooooodo加框线 
        self.createTop()  
        self.createBtn()      
        self.lstMessage = wx.ListBox(self, -1, size=(360, -1), style=wx.LB_SINGLE)
        self.layout()
        
        self.Bind(wx.EVT_BUTTON, self.OnAddClick, self.btnAdd)   
        self.Bind(wx.EVT_BUTTON, self.OnEditClick, self.btnEdit) 
        self.Bind(wx.EVT_BUTTON, self.OnDelClick, self.btnDel)
        self.Bind(wx.EVT_BUTTON, self.OnSaveClick, self.btnSave)
        self.Bind(wx.EVT_LISTBOX, self.OnItemSelect, self.lstMessage)
        
        self.loadMessageData()
        
    def createTop(self):
        def createImgs():
            l = []
            for index, name in enumerate(images.imgs):
                bmp = name.GetBitmap()
                btn = wx.BitmapButton(self, 1001 + index, bmp, (20, 20), (bmp.GetWidth() + 10, bmp.GetHeight() + 10), name='23')
                self.Bind(wx.EVT_BUTTON, self.OnBtnImgClick, btn)
                l.append(btn)
            return l
        self.imgs = createImgs()
        
        self.txtMessage = wx.TextCtrl(self, -1, "", size=(200, -1), style=wx.TE_MULTILINE | wx.TE_PROCESS_ENTER)
        self.txtMessage.SetFocus()
        
    def createBtn(self):
        self.btnAdd = wx.Button(self, -1, u'添加')
        self.btnEdit = wx.Button(self, -1, u'修改')
        self.btnEdit.Enable(False)
        self.btnDel = wx.Button(self, -1, u'删除')
        self.btnDel.Enable(False)
        self.btnSave = wx.Button(self, -1, u'保存到数据库')
        
    def createBtnSizer(self):
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer.Add(self.btnAdd, 0, wx.ALL, 5)
        btnSizer.Add(self.btnEdit, 0, wx.ALL, 5)
        btnSizer.Add(self.btnDel, 0, wx.ALL, 5)
        btnSizer.Add(self.btnSave, 0, wx.ALL, 5)
        return btnSizer
   
    def layout(self):
        box = wx.StaticBox(self, -1, u'迷你博客留言管理')
        bSizer = wx.StaticBoxSizer(box, wx.VERTICAL) 
        
        imgSizer = wx.BoxSizer(wx.HORIZONTAL)
        for img in self.imgs:
            imgSizer.Add(img)
        
        bSizer.Add(imgSizer, 0, wx.ALL | wx.EXPAND, 10)         
        bSizer.Add(self.txtMessage, 0, wx.ALL | wx.EXPAND, 10)         
        btnSizer = self.createBtnSizer()   
        bSizer.Add(btnSizer, 0, wx.ALL | wx.EXPAND, 10)  
              
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(bSizer, 0, wx.ALL | wx.EXPAND, 5)
        mainSizer.Add(self.lstMessage, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self) 
         
    ################################## 
    def afterModify(self):
        self.txtMessage.Clear()
        self.txtMessage.SetFocus()        
        self.btnEdit.Enable(False)
        self.btnDel.Enable(False) 
                   
    def OnAddClick(self, event):
        name = self.txtMessage.GetValue().strip()
        if name:
            if self.lstMessage.FindString(name) == -1:
                self.lstMessage.Insert(name, 0)
            else:
                wx.MessageBox(u'留言已存在', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION) 
            self.afterModify() 
        else:
            wx.MessageBox(u'留言不得为空', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION) 
        
    def OnEditClick(self, event):         
        message = self.txtMessage.GetValue().strip()
        if message:
            items = self.lstMessage.GetItems()
            items.remove(self.lstMessage.GetStringSelection())
            if not message in items:
                index = self.lstMessage.GetSelection()
                self.lstMessage.SetString(index, message)
                self.lstMessage.SetSelection(-1)
            else:
                wx.MessageBox(u'留言已存在', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION) 
            self.afterModify() 
        else:
            wx.MessageBox(u'留言不得为空', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION) 
    
    def OnDelClick(self, event):
        index = self.lstMessage.GetSelection()
        self.lstMessage.Delete(index)
        self.afterModify() 

    def OnSaveClick(self, event):
        messages = self.lstMessage.GetItems()
        Tools.saveData(r'db/tmessage', messages)
        wx.MessageBox(u'保存成功', u'友情提示', style=wx.OK | wx.ICON_EXCLAMATION)
    
    def OnItemSelect(self, event):
        message = self.lstMessage.GetStringSelection()
        self.txtMessage.SetValue(message)
        self.btnEdit.Enable(True)
        self.btnDel.Enable(True)
        
    def OnBtnImgClick(self, event):
        id = event.Id - 1000
        self.txtMessage.WriteText('[em%s]' % id)
        
    def loadMessageData(self):
        messages = Tools.loadData(r'db/tmessage')
        if messages:
            for m in messages:
                self.lstMessage.Append(m)
                 
if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()
